<?php
namespace Admin\Controller;
use Admin\Controller\CheckController;
class ExpressController extends AdminController{
	//快递
  protected function _initialize()
  {
		parent::_initialize ();
	}
	public $express_company = [1=>'顺丰快递','圆通快递','中通快递','申通快递','韵达快递','邮政快递'];//快递公司

	public function index()
  {
		$this->display();
	}

	public function add_index($p=1)
  {
    $p        = intval($p) > 0 ? $p : 1;
    $user     = M('user u');
    $pagesize = 15;//每页数量
    $offset   = $pagesize * ($p - 1);//计算记录偏移量

    $where = '1 = 1';
		if(I('get.aid', -1, 'intval') <> -1)
		{
			if(I('get.aid', -1, 'intval')==0){
				$where .=' and  aid =0';
			}else{
				$where .=' and aid='.I('get.aid', 0, 'intval');
			}
		}
		$where .= I('get.user_name') != '' ? ' and user_name like "%'.I('get.user_name').'%"' : '';

		$where .= I('get.telephone') != '' ? ' and telephone = '.I('get.telephone') : '';

		$where .= I('get.create_time_beg') != '' ? ' and create_time >= '.strtotime(I('get.create_time_beg')) : '';

		$where .= I('get.create_time_end') != '' ? ' and create_time < '.strtotime(I('get.create_time_end')) : '';
		$where .= I('get.status') !=''? ' and u.status='.I('get.status') : '';
    if(session('adminInfo.gpid')!=0){
        $groupInfo=$this->getInfo('*','group','id='.session('adminInfo.gpid'));
        if($groupInfo['work_status']==1){
           $where.=' and gpid='.session('adminInfo.gpid');
        }
    }

    $count = $user->join('left join app_bank b on u.loan_bank_id=b.id')->where($where)->count();
    $list  = $user->field('u.*,b.bank_short_name')->join('left join app_bank b on u.loan_bank_id=b.id')->where($where)->order('u.id desc')->limit($offset . ',' . $pagesize)->select();
    $page  = new \Think\Page($count, $pagesize);
    $page  = $page->show();

    foreach ($list as $key => $value)
    {
			$group=$this->getInfo('*','group','id='.$value['gpid']);
			$list[$key]['group_name']=$group['name'];
			$express=$this->getInfo('*','express','user_id='.$value['id'].' and status>=2');
            if(!$express)
            {
                $arr[$key]['id']              = $value['id'];
                $arr[$key]['entry_no']        = $value['entry_no'];
                $arr[$key]['user_name']       = $value['user_name'];
                $arr[$key]['identity_no']     = $value['identity_no'];
                $arr[$key]['telephone']       = $value['telephone'];
                $arr[$key]['department_name'] = $value['department_name'];
                $arr[$key]['create_time']     = $value['create_time'];
                $arr[$key]['update_time']     = $value['update_time'];
                $arr[$key]['bank_short_name'] = $value['bank_short_name'];
             }

		}
		$this->assign('list', $arr);
    $this->assign('page', $page);
		$this->assign('statusItems', $this->statusItems);
		$this->display();
	}


	public function add_info()
  {
		if(IS_POST)
    {
           $data=array(
             'user_id'         => $_POST['user_id'],
             'admin_id'        => session('adminInfo.id'),
             'send_name'       => $_POST['send_name'],//寄件人
             'addressee'       => $_POST['addressee'],//收件人
             'user_name'       => $_POST['user_name'],//客户姓名
             'express_number'  => $_POST['express_number'],//快递单号
             'file'            => implode(',',$_POST['file']),//快递内容（文件）
             'company_id'      => $_POST['company_id'],//快递公司
             'express_company' => $this->express_company($_POST['company_id']),
             'express_times'   => strtotime($_POST['express_times']),//快递时间
             'add_times'       => time(),//创建时间
             'update_times'    => time(),//更新时间
             'status'          => 2,
             'img'             => $_POST['img'],
           	);
           $express=$this->getInfo('*','express','user_id='.$_POST['user_id']);

           if(!$express)
           {
              $result=M('express')->add($data);
              $express_id=$result;
           }

           if($result)
           {
              $log = array('user_id'=>$_POST['user_id'],'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>102,'opinion'=>'快递已录入','note'=>'快递已录入','add_time'=>time(),'express_id'=> $express_id,'send_name'=>$_POST['send_name'],'addressee'=>$_POST['addressee'],'user_name'=>$_POST['user_name'],'express_number'=>$_POST['express_number'],'file'=>implode(',',$_POST['file']),'express_times'=>strtotime($_POST['express_times']));
              M('check_log')->add($log);
              $check_data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'));
              M('user')->where('id='.$_POST['user_id'])->save($check_data);
              redirect('/Admin/Express/add_index');
           }else{
            	$this->error('快递录入失败',U('Express/add_index'),0);
           }
		}
  		$id=I('get.user_id',0,'intval');
  		$user=$this->getInfo('*','user','id='.$id);
  		$info=$this->getInfo('*','express','user_id='.$id);
  		$info['file'] = explode(',', $info['file']);
  		$this->assign('express_company',$this->express_company);
  		$this->assign('user',$user);
  		$this->assign('info',$info);
  		$this->display();
	}


  //快递确认(渠道内勤)
  public function confirm()
  {
     $where = '1 = 1';
     if(session('adminInfo.gpid')!=0)
     {
       $groupInfo=$this->getInfo('*','group','id='.session('adminInfo.gpid'));
       if($groupInfo['work_status']==1){
         $where.=' and gpid='.session('adminInfo.gpid');
       }
   }
      $where.=' and check_status=22 and (u.status!=-1)';
      $list=$this->getList('u.*,b.bank_short_name','user u',$where,'id desc',true,'left join app_bank b on u.loan_bank_id=b.id');
      foreach ($list as $key => $value) {
        $group=$this->getInfo('*','group','id='.$value['gpid']);
        $list[$key]['group_name']=$group['name'];
      }
      $this->assign('list',$list);
      $this->display();

   }


   public function confirm_info()
   {
      if(IS_POST)
      {
          $express=$this->getInfo('*','express','user_id='.$_POST['user_id'].' and status=4');
          if($express['confirm_file']!='')
          {
            $data['confirm_file']=$express['confirm_file'].','.implode(',',$_POST['file']);
          }else{
            $data['confirm_file']=implode(',',$_POST['file']);
          }
          if($_POST['confirm_type']==1)
          {
            $data['status']=4;
          }else{
            $data['status']=11;
          }

          $data['update_time'] = time();
          $data['recipient'] = $_POST['recipient'];
          $result = M('express')->where('user_id='.$_POST['user_id'].' and status=4')->save($data);

          if($_POST['confirm_type']==0)
          {
               $log=array('user_id'=>$_POST['user_id'],'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>6,'opinion'=>'快递确认(渠道内勤)','note'=>'快递确认(渠道内勤)','add_time'=>time(),'file'=>implode(',',$_POST['file']),'check_status'=>23);
               M('check_log')->add($log);
               $check_data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'),'status'=>10,'check_status'=>23);
               M('user')->where('id='.$_POST['user_id'])->save($check_data);
               redirect('/Admin/Express/confirm');
         }else{
            $log=array('user_id'=>$_POST['user_id'],'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>6,'opinion'=>'快递确认待定(渠道内勤)','note'=>$_POST['note'],'add_time'=>time(),'file'=>implode(',',$POST['file']));
             M('check_log')->add($log);
             $check_data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'),'status'=>9);
             M('user')->where('id='.$_POST['user_id'])->save($check_data);
             $this->error('快递确认待定',U('Express/confirm'));
         }
      }else{
         $user_id=I('get.user_id',0,'intval');
         $info=$this->getInfo('*','express','user_id='.$user_id.' and status=4');
         $info['confirm_file'] = explode(',', $info['confirm_file']);
         $user=$this->getInfo('*','user','id='.$user_id);
         $this->assign('user',$user);
         $this->assign('info',$info);
         $this->display();
      }

    }

    public function Contract()
    {
       if($_POST['act']==1)
       {
           $id=explode(',',$_POST['ids']);
           $tems  = 0;
           foreach ($id as $v)
           {
             if($v!=0){
                  $user=$this->getInfo('*','user','id='.$v.' and check_status!=16');
                  if($user){
                   $data['user_id']         = $v;
                   $data['user_name']       = $user['user_name'];
                   $data['admin_id']        = session('adminInfo.id');
                   $data['send_name']       = $_POST['send_name'];
                   $data['addressee']       = $_POST['addressee'];
                   $data['express_number']  = $_POST['express_number'];
                   $data['file']            = $_POST['express_content'];
                   $data['company_id']      = $_POST['company_id'];
                   $data['express_company'] = $this->express_company($_POST['company_id']);
                   $data['express_times']   = strtotime($_POST['express_times']);
                   $data['add_times']       = time();
                   $data['update_times']    = time();
                   $data['status']          = 12;
                   $result                  = M('express')->add($data);

                   $log=array('user_id'=>$v,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>102,'opinion'=>'银行合同已送回(银行驻点)','note'=>'银行合同已送回(银行驻点)','add_time'=>time(),'check_status'=>16,'express_id'=> $result,'send_name'=>$_POST['send_name'],'addressee'=>$_POST['addressee'],'user_name'=>$user['user_name'],'express_number'=>$_POST['express_number'],'file'=>$_POST['express_content'],'express_times'=>strtotime($_POST['express_times']));
                   M('check_log')->add($log);
                   $check_data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'),'check_status'=>16);
                   M('user')->where('id='.$v)->save($check_data);
                 }else{
                    $this->error('该文件已寄出');
                 }
             }
                $tems++;

           }
            $this->success("批量执行成功");

       }else{
            $id=explode(',',$_POST['ids']);
            $tems  = 0;
            foreach ($id as $v)
            {
              if($v!=0){
                   $log=array('user_id'=>$v,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>6,'opinion'=>'银行合同已送回(银行驻点)','note'=>'银行合同已送回(银行驻点)','add_time'=>time(),'check_status'=>16,'addressee'=>$_POST['addressee']);
                   M('check_log')->add($log);
                   $check_data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'),'check_status'=>16);
                   M('user')->where('id='.$v)->save($check_data);
              }
              $tems++;
           }
            $this->success("批量执行成功");
       }
    }

   public function express_company($company_id)
   {
       switch ($company_id) {
       	case '1':
       		$company_name='顺丰快递';
       		break;
       	case '2':
       		$company_name='圆通快递';
       		break;
       	case '3':
       		$company_name='中通快递';
       		break;
       	case '4':
       		$company_name='申通快递';
       		break;
       	case '5':
       		$company_name='韵达快递';
       		break;

       	default:
       		$company_name='未知操作';
       		break;
       }
       return $company_name;
       exit;
   }

}